<?php
class Category_model extends Base_Model
{

    public function __construct()
    {
        $this->load->database();
    }


    /**
     * 获取线路(路线、活动和自营)类型
     */
    public function all_categories($data = array())
    {
        $query = $this->db->get('tiyu_category')->result_array();
        //将子类并入直接父类
        $ret = array();
        foreach ($query as $rs)
        {
            $id   = $rs['id'];
            $name = $rs['name'];
            $type = $rs['type'];
            $pid  = $rs['parentId'];

            if ($type == 'MAINCAT')
            {
                $ret[$id] = array(
                    'id' => $id,
                	'name'=>$name,
					'parentId'=>$pid,
					'child' => array(),
                );
            }
            else
            {
                $ret[$pid]['child'][$id]['id'] = $id;
                $ret[$pid]['child'][$id]['name']=$name;
            }
        }
        
        $res=array();
        foreach($ret as $id=>$v)
        {
        	//去掉最顶级分类，保留子类的直接主类
        	if(!empty($v['child']) && $v['parentId']==$data['parentId'])
        	{
        		$res[$id]['id']=$v['id'];
        	    $res[$id]['name']=$v['name'];
        	    $res[$id]['child']=array_values($v['child']);
			    //$res[$id]['child'][]=array('id'=>'0','name'=>'不限');
			    //$res[$id]['child']=array_reverse($res[$id]['child']);
			}        	
        }
        
        return array_values($res);
    }
}
